//==========================================================================================================
//			PROGRAMM FOR THE MAVDEK IDU 4-5 HP
//			
//==========================================================================================================

#define M_A_I_N
#include "global.h"
__CONFIG(HS&WDTDIS&PWRTEN&BORDIS&LVPDIS&DUNPROT&WRTEN&DEBUGEN&UNPROTECT);
//==========================================================================================================
//
//			Headers to be included here
//
//==========================================================================================================


//==========================================================================================================
//
//			constant declaration
//
//==========================================================================================================

//const char* const myMsgFree={"                    "};
//const char* const myMsgGroup[]={"invalid","Idu","Odu","kCOM","MSMP"};
//const char* const myMsgStatCmd[]={"Req ","OK","Done","Proc","BadOpCode","BadAddr","BadData"};
//const char* const myMsgOpCode[]={"hCOM","MemRead","MemWrite"," ITU ","RemoteData","RemoteData"};

const char const ITUHeader[4]={0xfe,0b00000010,0x03,0x6c};
const char const ITUPreset[6]={0,0,0x64,0x00,0x3c,0x09};


const char const IDUHeader[5]={0xfe,0x02,0x01,0x1a,0x08};
const char const IDUPreset[20]={0x00,0x00,0x00,0x0b,0x00,0x00,0x49,0x1c,0x10,0x10,0x19,0x1a,0x16,0x00,0x18,0x19,0xec,0x18,0x00,0x00};
bank2 unsigned char ITUMsgId;
bank2 unsigned char IDUMsgId;

//unsigned char Temp_CommunicationState;


//=========================================================================================================
//
//			global variables initialization
//
//=========================================================================================================

bank1 union ComHeaderType MyODUIDUHeader;
bank1 union ComHeaderType MyIDUHeader;
bank1 union ComHeaderType MyODUkCOMHeader;
bank2 union ODUType MyODU;
bank2 union IDUType MyIDU;
bank2 union ITUType MyITU;
bank2 union ITUType MyITU_answer;
bank1 struct PresetType myPreset;
bank1 struct PresetType myAnswer;

bank2 unsigned char counter1, button;

volatile bank1  en_group sourse,destination;

bit flag_pulse_tick;
bit flag_system_tick;
bit	flag_packet_received;	 	
bit	flag_packet_good;
bit	flag_packet_sent;
bit	flag_com_pause;
bit	flag_rx_error;
bit flag_test_running=0;

union FLAGS flag_packet_tov;

bank2 int test_duration,rest_of_test,rest_of_stage;
//=========================================================================================================
//
//			Function section
//
//=========================================================================================================



void PresetToITU (void)
{
	MyITU.Data.OOprEEV  = myPreset.EEV_value;
	MyITU.Data.CompFreq = myPreset.Compressor_freq;
	MyITU.Data.OOprOFan = myPreset.Fan_speed;
	MyITU.Data.IOprMMode = myPreset.Mode;
};

void ITUtoAnswer(void)
{
	myAnswer.EEV_value = MyITU_answer.Data.OOprEEV & 0x03ff;
	myAnswer.Compressor_freq = MyITU_answer.Data.CompFreq;
	myAnswer.Fan_speed=MyITU_answer.Data.OOprOFan;
	myAnswer.Mode = MyITU_answer.Data.IOprMMode ;
};

unsigned char Check_answer (void)
{	unsigned char answer =0;
	if (myAnswer.EEV_value != myPreset.EEV_value) answer& =1;
	if (myAnswer.Compressor_freq!=myPreset.Compressor_freq) answer& =2;
	if (myAnswer.Fan_speed != myPreset.Fan_speed) answer&=4;
	if (	myAnswer.Mode !=myPreset.Mode) answer &=8;
	return answer;
}

// extern void DoWork(void);

//=============================================================================================================
//				M		M    AAA     II  N     N
//				M M   M	M	A	A	 II  N N   N
//				M  M M	M  A     A   II  N  N  N
//				M   M   M  AAAAAAA   II  N   N N
//				M       M  A     A   II  N    NN
//				M       M  A     A   II  N     N
//=============================================================================================================
void main (void)
{ 
	unsigned char i;
//	unsigned char m_verdict;
	static bank3 unsigned int main_timeout_counter=0;
		InitIO();
		PIE1=PIE2=INTCON=0;
		init_comms();
		TXIE=0;
		TXEN=1;
		RCIE=1;
		InitTMR1();
		InitTMR0();
		ADCInit();

		GIE=1;
		resetTime();
		InitLCD();
		CursorOffLCD();
		ClearLCD();


for ( i=0; i<6;i++)	
{	MyITU.access[i]=ITUPreset[i];
}

for ( i=0; i<20;i++)	
{	MyIDU.access[i]=IDUPreset[i];
}

ClearLCD();
resetTime();

//===================================================================================================
//				E N D L E S S    L O O P
//===================================================================================================
Red_lamp_off();
Green_lamp_on(); 

while (1)
	{	if (flag_pulse_tick) 		// every 40ms
		{	
		
			if (StartButtonPressed())
			{	flag_test_running=1;
			};

			if (StopButtonPressed())
			{ 	flag_test_running=0;
				InitIO();
				ClearLCD();
			};

			if (flag_packet_tov.Task.Main)
			{	flag_packet_tov.Task.Main=0;
				PresetToITU ();
				ITUtoAnswer();
				if (!Check_answer) StopITUComm();
			}; // END of packet good

			flag_pulse_tick=0;
		
		};	// end of pulse tick 40ms


		if (flag_system_tick)	// once in 250ms
		{	
			ADC_CurrentService(); // calculate Current parameters
			DoTest();
			flag_system_tick=0;
		}; //end of system tick  250ms
		
	}; // end of endless loop
};	// end of main













